library(tidyverse)
library(magrittr)
library(data.table)
library(lubridate)
library(hms)
library(ggthemes)
library(modelsummary)
library(rdrobust)
library(rddensity)

theme_set(theme_few())

### SET WORKING DIRECTORY HERE ###
path_to_archive <- "replication/"
data_dir <- paste0(path_to_archive, "data/")
setwd(data_dir)
plot_dir <- paste0(path_to_archive, "plots/")
tables_dir <- paste0(path_to_archive, "tables/")

rd <- readRDS(paste0(data_dir, "all_rd_data.rds"))


### graphically first ###
rd[,news_diff := news_post - news_pre]
rd[,tunein_delta := round(tunein_delta,0)]


## plot against running variable (tunein time)
## for T group (tunein_delta <=0) versus C2/C3 groups (tunein_delta > 0)
rdbin <- rd[group != "C1",		
			c(map(.SD, mean), map(.SD, sd), `Number of Devices`=.N),
			by=.(tunein_delta), 
			.SDcols = c("news_pre", "news_post", "news_diff")]

colnames(rdbin)[5:7] <- paste(colnames(rdbin)[5:7], "sd", sep="_")

rdbin[,`Saw Ad?`:= if_else(tunein_delta <= 0, "Y", "N")]

### FIGURE 4(A)
ggplot(aes(x = tunein_delta, y = news_diff, group = `Saw Ad?`), data=rdbin) +
	geom_point(aes(alpha=`Number of Devices`, shape=`Saw Ad?`)) + 
	geom_smooth(method='lm', formula = y ~ poly(x,2), colour = "grey20") +
	labs(x="Tune-in time relative to ad time (seconds)", y = "24h Change in News Viewing (seconds)") +
	lims(y=c(-250, 950)) +
	theme(legend.position = "bottom",
		  legend.box.background=element_rect())
ggsave(paste0(plot_dir, "rd_news_diff.pdf"), width=8, height=6)

### FIGURE 3(A)
ggplot(aes(x = tunein_delta, y = news_post, group = `Saw Ad?`), data=rdbin) +
	geom_point(aes(alpha=`Number of Devices`, shape=`Saw Ad?`)) + 
	geom_smooth(method='lm', formula = y ~ poly(x,2), colour = "grey20") +
	labs(x="Tune-in time relative to ad time (seconds)", y = "News Viewing in 24h after ad time (seconds)") +
	theme(legend.position = "bottom",
		  legend.box.background=element_rect())
ggsave(paste0(plot_dir, "rd_news_post.pdf"), width=8, height=6)

### FIGURE 3(B)
ggplot(aes(x = tunein_delta, y = news_pre, group = `Saw Ad?`), data=rdbin) +
	geom_point(aes(alpha=`Number of Devices`, shape=`Saw Ad?`)) + 
	geom_smooth(method='lm', formula = y ~ poly(x,2), colour = "grey20") +
	labs(x="Tune-in time relative to ad time (seconds)", y = "News Viewing in 24h before ad time (seconds)") +
	theme(legend.position = "bottom",
		  legend.box.background=element_rect())
ggsave(paste0(plot_dir, "rd_news_pre.pdf"), width=8, height=6)


rdbin0 <- rd[group!="T" & tunein_delta >= -1200,c(map(.SD, mean), map(.SD, sd), `Number of Devices`=.N),by=.(tunein_delta), .SDcols = c("news_pre", "news_post", "news_diff")]
colnames(rdbin0)[5:7] <- paste(colnames(rdbin0)[5:7], "sd", sep="_")

rdbin0[,`Control Group`:= if_else(tunein_delta > 0,  "Tuned in After", "Tuned out Before")]


### FIGURE 4(B)
ggplot(aes(x = tunein_delta, y = news_diff, group = `Control Group`), data=rdbin0) +
	geom_point(aes(alpha=`Number of Devices`, shape=`Control Group`)) + 
	geom_smooth(method='lm', formula = y ~ poly(x,2), colour = "grey20") +
	labs(x="Tune-in time relative to ad time (seconds)", y = "24h Change in News Viewing (seconds)") +
	theme(legend.position = "bottom",
		  legend.box.background=element_rect()) +
	lims(y=c(-250, 950)) +
	guides(alpha = guide_legend(order = 1), 
           shape = guide_legend(order = 2))
ggsave(paste0(plot_dir, "rd_news_diff_placebo.pdf"), width=8, height=6)
